### NYC Public Employees Minimum Wage Voter Turnout
### This file replicates our main analysis
### But modifies the cutpoint used for defining who's MW-affected (see SI Section 3.7)
### main approach always relies on pre-2014 employment/wage relative to first MW increase ($8) for a tidy, consistent pre-treatment measure of who's affected
### but we think it's also defensible to adjust the "affected" group based on the exact increases that took place over the period of the particular diff-in-diff, which is what we show here.

#load some packages
library(tidyverse)
library(data.table)
library(feather)
library(sandwich)
library(lmtest)
library(ggplot2)
library(ggthemes)
library(stargazer)
library(plm) 
library(xtable)
library(lfe)
library(broom)

n_boot <- 100

setwd('~/Dropbox (MIT)/Minimum Wage Voter Turnout/Replication Materials')

everyone <- data.table(read_feather("Data/Voters and Employees, Dropped Dead and Children, Counties and Age as Tie Breakers_newmerge2018file.feather"))

everyone[, minsalary:= min(BaseSalary.2011,BaseSalary.2012, BaseSalary.2013, BaseSalary.2014, na.rm=T), by=list(employeekey, PayBasis)]
everyone[, MaxSalary:= max(BaseSalary.2011, BaseSalary.2012, BaseSalary.2013, BaseSalary.2014, na.rm=T), by=list(employeekey, PayBasis)]
everyone <- everyone[!(minsalary <7.25),]

##add the undermin variable (this is what's different in this script)
# use the same $8 cutoff as in the main paper for the 2010-2014 analysis
everyone[!is.na(BaseSalary.2011) | !is.na(BaseSalary.2012) | !is.na(BaseSalary.2013) | !is.na(BaseSalary.2014), UnderMin14:= FALSE] 
everyone[(everyone$'BaseSalary.2011' < 8.00 | everyone$'BaseSalary.2012' < 8.00 | 
            everyone$'BaseSalary.2013' < 8.00 | everyone$'BaseSalary.2014' < 8.00), UnderMin14:=TRUE]

#now go up to 9 for the 2012-2016 analysis
everyone[!is.na(BaseSalary.2011) | !is.na(BaseSalary.2012) | !is.na(BaseSalary.2013) | !is.na(BaseSalary.2014), UnderMin16:= FALSE] 
everyone[(everyone$'BaseSalary.2012' < 9.00 | 
            everyone$'BaseSalary.2013' < 9.00 | everyone$'BaseSalary.2014' < 9.00|
            everyone$'BaseSalary.2015' < 9.00 | everyone$'BaseSalary.2016' < 9.00), UnderMin16:=TRUE]

#and up to 11 for the mayoral analysis
everyone[!is.na(BaseSalary.2011) | !is.na(BaseSalary.2012) | !is.na(BaseSalary.2013) | !is.na(BaseSalary.2014), UnderMin17:= FALSE] 
everyone[(everyone$'BaseSalary.2013' < 11.00 | everyone$'BaseSalary.2014' < 11.00 |
            everyone$'BaseSalary.2015' < 11.00 | everyone$'BaseSalary.2016' < 11.00 | 
            everyone$'BaseSalary.2017' < 11.00), UnderMin17:=TRUE]




#function to fit 2016 v 2012 panels for a given dataset
fit_mod_16 <- function(df){
  long <- data.frame(employee=rep(df$'employeekey',2), #salary = rep(df$'BaseSalary.2016',2),
                     underMin = rep(df$'UnderMin16', 2),  voted = c(df$'Voted16', df$'Voted12'),
                     year = c(rep(16, nrow(df)), rep(12, nrow(df))))
  
  model <- felm(voted~underMin*factor(year)|0|0|employee, data=long)
  return(model)
}


everyone_under15 <- fit_mod_16(everyone[(everyone$PayBasis == "per Hour" & everyone$MaxSalary < 15),])
everyone_under15_se <- sqrt(diag(vcov(everyone_under15)))

everyone_all <- fit_mod_16(everyone[(everyone$PayBasis == "per Hour"),])
everyone_all_se <- sqrt(diag(vcov(everyone_all)))




#function to fit 2014 v 2010 panels for a given dataset
fit_mod_14 <- function(df){
  long <- data.frame(employee=rep(df$'employeekey',2),
                     underMin = rep(df$'UnderMin14', 2),  voted = c(df$'Voted14', df$'Voted10'),
                     year = c(rep(14, nrow(df)), rep(10, nrow(df))))
  
  model <- felm(voted~underMin*factor(year)|0|0|employee, data=long)
  return(model)
}
everyone14_all <- fit_mod_14(everyone[(everyone$PayBasis == "per Hour"),])
everyone14_all_se <- sqrt(diag(vcov(everyone14_all)))

everyone14_under15 <- fit_mod_14(everyone[(everyone$PayBasis == "per Hour" & everyone$MaxSalary < 15),])
everyone14_under15_se <- sqrt(diag(vcov(everyone14_under15)))


#function to fit 2017 v 2013 panels for a given dataset
fit_mod_17 <- function(df){
  long <- data.frame(employee=rep(df$'employeekey',2), salary = rep(df$BaseSalary.2014,2),
                     underMin = rep(df$'UnderMin17', 2),  voted = c(df$'Voted17', df$'Voted13'),
                     year = c(rep(17, nrow(df)), rep(13, nrow(df))))
  
    model <- felm(voted~underMin*factor(year)|0|0|employee, data=long)
  return(model)
}

everyone17_all <- fit_mod_17(everyone[(everyone$PayBasis == "per Hour"),])
everyone17_all_se <- sqrt(diag(vcov(everyone17_all)))
everyone17_all_se <- sqrt(diag(vcov(everyone17_all)))

everyone17_all_se <- sqrt(diag(vcov(everyone17_all)))
everyone17_under15 <- fit_mod_17(everyone[(everyone$PayBasis == "per Hour" & everyone$MaxSalary < 15),])
everyone17_under15_se <- sqrt(diag(vcov(everyone17_under15)))


mods <- do.call(rbind, lapply(list(everyone_all, everyone_under15,everyone14_all, everyone14_under15, everyone17_all, everyone17_under15),tidy))
mods$se <- c(everyone_all_se, everyone_under15_se, everyone14_all_se, everyone14_under15_se, everyone17_all_se, everyone17_under15_se)
mods <- mods[grepl(':', mods$term),]
mods$Election <- c('Presidential (2012-2016)', 'Presidential (2012-2016)', 'Midterm (2010-2014)', 
                   'Midterm (2010-2014)', "Mayoral (2013-2017)", 'Mayoral (2013-2017)')
mods$lower <- mods$estimate - 1.96*mods$se
mods$upper <- mods$estimate + 1.96*mods$se
mods$'Employees Included' <- factor(rep(c('All', 'Under $15 Per Hour'), 3))


pdf("Figures/alternate_cutpoints_main_results.pdf", height=3.5, width=8)
ggplot(data=mods, aes(y=estimate, ymin=lower, ymax=upper, x=Election, 
  shape=`Employees Included`, color=`Employees Included`)) + 
  geom_errorbar(position=position_dodge2(width=.5,reverse=T), width=.5) + 
  geom_point(size=3, position=position_dodge2(width=.5, reverse=T)) +
  geom_hline(alpha=.5, yintercept=0, linetype='dashed') +coord_flip() + theme_few() +
  ylab(expression(atop("Increase in Turnout From Minimum Wage Increase", paste("(Percentage Points)")))) + xlab('') +
  theme(axis.text.y = element_text(colour = "black"))
dev.off()
